/*
Copyright 2010-2011 CumulusCloud
http://cumuluscloud.cc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package appmanager.client.views;

import java.util.List;

import appmanager.client.AppService;
import appmanager.client.AppServiceAsync;
import appmanager.client.tablecell.ConfirmDelegate;
import appmanager.shared.vo.LocalApp;

import com.google.gwt.cell.client.ActionCell;
import com.google.gwt.cell.client.ActionCell.Delegate;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class InstalledApplicationsView extends Composite {
	interface InstalledApplicationsViewUiBinder extends	UiBinder<Widget, InstalledApplicationsView> {}
	private static InstalledApplicationsViewUiBinder uiBinder = GWT.create(InstalledApplicationsViewUiBinder.class);
	private final AppServiceAsync appService = GWT.create(AppService.class);
	
	@UiField CellTable<LocalApp> table;
	
	private AsyncCallback<List<LocalApp>> retrievedLocalApps = new AsyncCallback<List<LocalApp>>() {
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Could not connect to server");
		}
		
		@Override
		public void onSuccess(List<LocalApp> result) {
			table.setRowCount(result.size());
			table.setRowData(0, result);
		}
	};
	
	private AsyncCallback<LocalApp> deleteCallback = new AsyncCallback<LocalApp>() {

		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Could not connect to server");
		}

		@Override
		public void onSuccess(LocalApp result) {
			Timer tmr = new Timer() {
				@Override
				public void run() {
					appService.getLocalAppList(retrievedLocalApps);
				}
			};
			tmr.schedule(10000);
		}
	};

	private Delegate<LocalApp> deleteDelegate = new Delegate<LocalApp>() {
		@Override
		public void execute(LocalApp object) {
			appService.delete(object, deleteCallback);
		}
	};

	private ActionCell<LocalApp> confirmDeletionCell = new ActionCell<LocalApp>("delete", new ConfirmDelegate<LocalApp>("Are you sure you want to delete that application?", deleteDelegate ));

	public InstalledApplicationsView() {
		initWidget(uiBinder.createAndBindUi(this));
		
		// The columns
		TextColumn<LocalApp> webnameColumn = new TextColumn<LocalApp>() {
			@Override
			public String getValue(LocalApp object) {
				return object.getWebname();
			}
		};
		TextColumn<LocalApp> hashColumn = new TextColumn<LocalApp>() {
			@Override
			public String getValue(LocalApp object) {
				return object.getHash();
			}
		};
		Column<LocalApp, LocalApp> deleteColumn = new Column<LocalApp, LocalApp>(confirmDeletionCell) {
			@Override
			public LocalApp getValue(LocalApp object) {
				return object;
			}
		};
		
		// Add the columns
		table.addColumn(webnameColumn, "Webname");
		table.addColumn(hashColumn, "Hash");
		table.addColumn(deleteColumn);
		
		// Retrieve the list
		appService.getLocalAppList(retrievedLocalApps);
	}
}
